using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Compiled;

namespace OR.Lazarus.Library.DataTier.Compiled.Lazarus
{
	/// <summary>
	/// Defines a Data Column within a Data Table, bound to a single entity field.
	/// </summary>
   public class DataColumnDetail : EntityBase
   {
		protected System.String mName;
		protected string mAlias;
		protected object mAutoGenerate;
		protected string mDataType;
		protected string mDefaultValue;
		protected string mFriendlyName;
		protected object mReadOnly;
		protected object mRequired;
		protected object mScale;
		protected object mSize;
		protected object mTableOnly;
		protected string mLogicalName;
		protected string mDBName;
		protected string mDBType;
		protected string mDescription;

		public DataColumnDetail()
		{
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Alias
		{
			get
			{
				return ( mAlias );
			}
			set
			{
				mAlias = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object AutoGenerate
		{
			get
			{
				return ( mAutoGenerate );
			}
			set
			{
				mAutoGenerate = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string DataType
		{
			get
			{
				return ( mDataType );
			}
			set
			{
				mDataType = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string DefaultValue
		{
			get
			{
				return ( mDefaultValue );
			}
			set
			{
				mDefaultValue = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string FriendlyName
		{
			get
			{
				return ( mFriendlyName );
			}
			set
			{
				mFriendlyName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object ReadOnly
		{
			get
			{
				return ( mReadOnly );
			}
			set
			{
				mReadOnly = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object Required
		{
			get
			{
				return ( mRequired );
			}
			set
			{
				mRequired = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object Scale
		{
			get
			{
				return ( mScale );
			}
			set
			{
				mScale = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object Size
		{
			get
			{
				return ( mSize );
			}
			set
			{
				mSize = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object TableOnly
		{
			get
			{
				return ( mTableOnly );
			}
			set
			{
				mTableOnly = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string LogicalName
		{
			get
			{
				return ( mLogicalName );
			}
			set
			{
				mLogicalName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string DBName
		{
			get
			{
				return ( mDBName );
			}
			set
			{
				mDBName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string DBType
		{
			get
			{
				return ( mDBType );
			}
			set
			{
				mDBType = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mAlias = DataUtility.GetString( pSource, "alias" );
			mAutoGenerate = DataUtility.GetBool( pSource, "autogenerate" );
			mDataType = DataUtility.GetString( pSource, "type" );
			mDefaultValue = DataUtility.GetString( pSource, "default" );
			mFriendlyName = DataUtility.GetString( pSource, "friendly-name" );
			mReadOnly = DataUtility.GetBool( pSource, "readonly" );
			mRequired = DataUtility.GetBool( pSource, "required" );
			mScale = DataUtility.GetInt32( pSource, "scale" );
			mSize = DataUtility.GetInt32( pSource, "size" );
			mTableOnly = DataUtility.GetBool( pSource, "tableonly" );
			mLogicalName = DataUtility.GetString( pSource, "logical-name" );
			mDBName = DataUtility.GetString( pSource, "dbname" );
			mDBType = DataUtility.GetString( pSource, "dbtype" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzc:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzc", "column", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mAlias ) )
			{
				vNewElement.SetAttribute( "alias", mAlias );
			}
					
			if ( mAutoGenerate != null )
			{
				if ( mAutoGenerate is System.Boolean )
				{
					vNewElement.SetAttribute( "autogenerate", mAutoGenerate.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mAutoGenerate.ToString() + " ] of type [ " + mAutoGenerate.GetType().ToString() + " ] is invalid.  The field [ AutoGenerate ] must be null or type [ System.Boolean ]." );
				}
			}
			
			if ( !String.IsNullOrEmpty( mDataType ) )
			{
				vNewElement.SetAttribute( "type", mDataType );
			}
					
			if ( !String.IsNullOrEmpty( mDefaultValue ) )
			{
				vNewElement.SetAttribute( "default", mDefaultValue );
			}
					
			if ( !String.IsNullOrEmpty( mFriendlyName ) )
			{
				vNewElement.SetAttribute( "friendly-name", mFriendlyName );
			}
					
			if ( mReadOnly != null )
			{
				if ( mReadOnly is System.Boolean )
				{
					vNewElement.SetAttribute( "readonly", mReadOnly.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mReadOnly.ToString() + " ] of type [ " + mReadOnly.GetType().ToString() + " ] is invalid.  The field [ ReadOnly ] must be null or type [ System.Boolean ]." );
				}
			}
			
			if ( mRequired != null )
			{
				if ( mRequired is System.Boolean )
				{
					vNewElement.SetAttribute( "required", mRequired.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mRequired.ToString() + " ] of type [ " + mRequired.GetType().ToString() + " ] is invalid.  The field [ Required ] must be null or type [ System.Boolean ]." );
				}
			}
			
			if ( mScale != null )
			{
				if ( mScale is System.Int32 )
				{
					vNewElement.SetAttribute( "scale", mScale.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mScale.ToString() + " ] of type [ " + mScale.GetType().ToString() + " ] is invalid.  The field [ Scale ] must be null or type [ System.Int32 ]." );
				}
			}
			
			if ( mSize != null )
			{
				if ( mSize is System.Int32 )
				{
					vNewElement.SetAttribute( "size", mSize.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mSize.ToString() + " ] of type [ " + mSize.GetType().ToString() + " ] is invalid.  The field [ Size ] must be null or type [ System.Int32 ]." );
				}
			}
			
			if ( mTableOnly != null )
			{
				if ( mTableOnly is System.Boolean )
				{
					vNewElement.SetAttribute( "tableonly", mTableOnly.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mTableOnly.ToString() + " ] of type [ " + mTableOnly.GetType().ToString() + " ] is invalid.  The field [ TableOnly ] must be null or type [ System.Boolean ]." );
				}
			}
			
			if ( !String.IsNullOrEmpty( mLogicalName ) )
			{
				vNewElement.SetAttribute( "logical-name", mLogicalName );
			}
					
			if ( !String.IsNullOrEmpty( mDBName ) )
			{
				vNewElement.SetAttribute( "dbname", mDBName );
			}
					
			if ( !String.IsNullOrEmpty( mDBType ) )
			{
				vNewElement.SetAttribute( "dbtype", mDBType );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzc", "description", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.DataColumnDetail v_oReturn = new Derived.DataColumnDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Alias = mAlias;
			v_oReturn.AutoGenerate = mAutoGenerate;
			v_oReturn.DataType = mDataType;
			v_oReturn.DefaultValue = mDefaultValue;
			v_oReturn.FriendlyName = mFriendlyName;
			v_oReturn.ReadOnly = mReadOnly;
			v_oReturn.Required = mRequired;
			v_oReturn.Scale = mScale;
			v_oReturn.Size = mSize;
			v_oReturn.TableOnly = mTableOnly;
			v_oReturn.LogicalName = mLogicalName;
			v_oReturn.DBName = mDBName;
			v_oReturn.DBType = mDBType;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections
			
			return ( v_oReturn );
		}

		public virtual Derived.DataColumnDetail CloneTyped()
		{
			return ( ( Derived.DataColumnDetail )this.Clone() );
		}
   }
}
   